/*
 * ULSF Sagittarius IDA
 * Copyright (C) 2012, John Pritchard, all rights reserved.
 * 
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */
package sagittarius.design;

import sagittarius.Field;
import sagittarius.Visualization;

/**
 * Asynchronous data mesh generator provides for dropping intermediate
 * mesh generation processes as UI values change.
 * 
 * Avoid regeneration on redundant data input.
 */
public class Generator
    extends Thread
{

    private volatile static Generator Current;


    private final Visualization data;


    Generator(Visualization data){
        super("sagittarius.design.Generator("+data+")");
        this.setDaemon(true);
        this.data = data;
        Generator current = Current;
        if (null != current && current.isAlive()){
            try {
                current.stop();
            }
            catch (Throwable t){
            }
        }
        Current = this;

        this.start();
    }


    public void run(){
        try {
            this.data.generate();
        }
        catch (Exception exc){
            exc.printStackTrace();
        }
        finally {
            this.data.terminal();
        }
    }
}
